package thread_try.question;

//public class AlternatePrinting {
//    private static int number = 0;
//    private static final Object lock = new Object();
//
//    public static void main(String[] args) {
//        System.out.println("use synchronize:");
//        // 线程A输出偶数（0, 2, 4...）
//        Thread threadA = new Thread(() -> {
//            while (number <= 200) {
//                synchronized (lock) {
//                    // 等待直到轮到自己（number为偶数）
//                    while (number % 2 != 0) {
//                        try {
//                            lock.wait();
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                    if (number > 200) break;
//                    System.out.println("A: " + number++);
//                    lock.notify(); // 唤醒线程B
//                }
//            }
//        });
//
//        // 线程B输出奇数（1, 3, 5...）
//        Thread threadB = new Thread(() -> {
//            while (number <= 200) {
//                synchronized (lock) {
//                    // 等待直到轮到自己（number为奇数）
//                    while (number % 2 == 0) {
//                        try {
//                            lock.wait();
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                    if (number > 200) break;
//                    System.out.println("B: " + number++);
//                    lock.notify(); // 唤醒线程A
//                }
//            }
//        });
//
//        threadA.start();
//        threadB.start();
//    }
//}

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class AlternatePrinting {
    private static int number = 0;
    private static final ReentrantLock lock = new ReentrantLock();
    private static final Condition conditionA = lock.newCondition();
    private static final Condition conditionB = lock.newCondition();
    private static boolean isATurn = true;

    public static void main(String[] args) {
        Thread threadA = new Thread(() -> {
            while (number <= 200) {
                lock.lock();
                try {
                    // 等待直到轮到自己
                    while (!isATurn || number > 200) {
                        conditionA.await();
                    }
                    System.out.println("A: " + number++);
                    isATurn = false;
                    conditionB.signal(); // 唤醒线程B
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        });

        Thread threadB = new Thread(() -> {
            while (number <= 200) {
                lock.lock();
                try {
                    // 等待直到轮到自己
                    while (isATurn || number > 200) {
                        conditionB.await();
                    }
                    System.out.println("B: " + number++);
                    isATurn = true;
                    conditionA.signal(); // 唤醒线程A
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        });

        threadA.start();
        threadB.start();
    }
}